from PyQt6.QtWidgets import (QDialog, QVBoxLayout, QHBoxLayout, QLabel, 
                           QLineEdit, QPushButton, QFileDialog)
from PyQt6.QtCore import Qt
import time
import uuid

class AddBookDialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("添加书籍")
        self.setFixedSize(400, 200)
        self.book_info = None
        self.init_ui()

    def init_ui(self):
        layout = QVBoxLayout(self)
        layout.setSpacing(16)
        layout.setContentsMargins(20, 20, 20, 20)

        # 书名输入
        title_layout = QHBoxLayout()
        title_label = QLabel("书名:")
        self.title_input = QLineEdit()
        title_layout.addWidget(title_label)
        title_layout.addWidget(self.title_input)
        layout.addLayout(title_layout)

        # 文件选择
        file_layout = QHBoxLayout()
        file_label = QLabel("文件:")
        self.file_path = QLineEdit()
        self.file_path.setReadOnly(True)
        browse_btn = QPushButton("浏览")
        browse_btn.clicked.connect(self.browse_file)
        file_layout.addWidget(file_label)
        file_layout.addWidget(self.file_path)
        file_layout.addWidget(browse_btn)
        layout.addLayout(file_layout)

        # 按钮区域
        btn_layout = QHBoxLayout()
        btn_layout.addStretch()
        
        cancel_btn = QPushButton("取消")
        cancel_btn.clicked.connect(self.reject)
        
        confirm_btn = QPushButton("确定")
        confirm_btn.clicked.connect(self.accept)
        confirm_btn.setDefault(True)
        
        btn_layout.addWidget(cancel_btn)
        btn_layout.addWidget(confirm_btn)
        layout.addLayout(btn_layout)

        # 设置样式
        self.setStyleSheet("""
            QDialog {
                background: #1E1E1E;
            }
            QLabel {
                color: white;
                font-size: 13px;
            }
            QLineEdit {
                background: #2D2D2D;
                border: 1px solid #3F3F3F;
                border-radius: 4px;
                color: white;
                padding: 6px;
                font-size: 13px;
            }
            QLineEdit:focus {
                border: 1px solid #5F5F5F;
            }
            QPushButton {
                background: #2D2D2D;
                border: 1px solid #3F3F3F;
                border-radius: 4px;
                color: white;
                padding: 6px 12px;
                font-size: 13px;
            }
            QPushButton:hover {
                background: #3D3D3D;
            }
            QPushButton:pressed {
                background: #4D4D4D;
            }
        """)

    def browse_file(self):
        file_name, _ = QFileDialog.getOpenFileName(
            self,
            "选择文件",
            "",
            "文本文件 (*.txt);;所有文件 (*.*)"
        )
        if file_name:
            self.file_path.setText(file_name)

    def get_book_info(self):
        """获取书籍信息"""
        if not self.title_input.text() or not self.file_path.text():
            return None
            
        return {
            'id': str(uuid.uuid4()),  # 生成唯一ID
            'title': self.title_input.text(),
            'file_path': self.file_path.text(),
            'progress': 0,  # 初始进度为0
            'last_read_time': int(time.time()),  # 当前时间戳
            'last_operation': '新增书籍',
            'add_time': time.strftime('%Y-%m-%d %H:%M:%S')
        } 